package org.blame.topic;

import com.rabbitmq.client.*;
import org.blame.constants.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Customer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //     1.建立连接
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost(Constants.HOST);
        connectionFactory.setPort(Constants.PORT);
        connectionFactory.setUsername(Constants.USER_NAME);
        connectionFactory.setPassword(Constants.PASSWORD);
        connectionFactory.setVirtualHost(Constants.VIRTUAL);

        Connection connection= connectionFactory.newConnection();

        //        2.开启信道
        Channel channel=connection.createChannel();
//              3. 申明交换机
        channel.exchangeDeclare(Constants.TOPIC_EXCHANGE_NAME, BuiltinExchangeType.TOPIC,true);
//              4.申明队列
        channel.queueDeclare(Constants.TOPIC_QUEUE_NAME2,true,false,false,null);
//              5.消费消息
        DefaultConsumer consumer=new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费消息+fanout+queue_name1"+new String(body));
            }
        };
        channel.basicConsume(Constants.TOPIC_QUEUE_NAME2,true,consumer);
    }
}
